package gui.panels.play;

import gui.actions.PlayAction;
import gui.actions.StatisticsAction;
import gui.components.FancyButton;
import gui.models.PlayModel;
import gui.utils.ComponentUtils;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JPanel;

import common.enums.AnswerEnum;

public class PlayButtonsPanel extends JPanel implements ActionListener {
 
    private static final long serialVersionUID = 1L;
    private static final int COMMON_GAP = 12;
    
    private final PlayModel model;
    private PlayAction playAction;
    
    private FancyButton yesButton;
    private FancyButton noButton;
    private FancyButton dontKnowButton;
    private FancyButton startGameButton;
    private FancyButton restartGameButton;
    private FancyButton gotoStatsticsButton;

    public PlayButtonsPanel(PlayModel model) {
    	this.model = model;
		this.playAction = new PlayAction(model);
        buildPanel();      
    }

	private void buildPanel() {
		yesButton = createPlayButton(PlayActionType.Yes);
        yesButton.addActionListener(this);
        yesButton.addActionListener(playAction);
        yesButton.setVisible(false);
        
        noButton = createPlayButton(PlayActionType.No);
        noButton.addActionListener(this);
        noButton.addActionListener(playAction);
        noButton.setVisible(false);
        
        dontKnowButton = createPlayButton(PlayActionType.DontKnow);
        dontKnowButton.addActionListener(this);
        dontKnowButton.addActionListener(playAction);
        dontKnowButton.setVisible(false);
        
        startGameButton = createPlayButton(PlayActionType.StartGame);
        startGameButton.addActionListener(this);
        startGameButton.addActionListener(playAction);
        startGameButton.setVisible(true);
        
        restartGameButton = createPlayButton(PlayActionType.RestartGame);
        restartGameButton.addActionListener(this);
        restartGameButton.addActionListener(playAction);
        restartGameButton.setVisible(false);
        
        gotoStatsticsButton = createPlayButton(PlayActionType.GotoStatstics);
        gotoStatsticsButton.addActionListener(new StatisticsAction(this));
        gotoStatsticsButton.setVisible(false);

        Box buttonsBox = Box.createHorizontalBox();
        
        buttonsBox.add(Box.createHorizontalStrut(COMMON_GAP));
        
        buttonsBox.add(yesButton);
        buttonsBox.add(gotoStatsticsButton);
        
        buttonsBox.add(Box.createHorizontalStrut(COMMON_GAP));
        
        buttonsBox.add(dontKnowButton);
        buttonsBox.add(startGameButton);
        buttonsBox.add(restartGameButton);
        
        buttonsBox.add(Box.createHorizontalStrut(COMMON_GAP));

        buttonsBox.add(noButton);
        
        buttonsBox.add(Box.createHorizontalStrut(COMMON_GAP));
        
        add(buttonsBox);
        
        setBorder(BorderFactory.createEmptyBorder(COMMON_GAP/2,0,0,0));
	}
    
    private FancyButton createPlayButton(final PlayActionType playActionType) {
        return new FancyButton(ComponentUtils.readImageIcon(playActionType.getIcon()), playActionType.getTitle(),true);
    }
    
    public void update() {
    	if (model.isGuessed()) {
    		if (model.isSubmitedGuess()) {
    			endGameState();
    		} else {
    			submitGuessState();
    		}
    	}
    }
    
    public void playState() {
        yesButton.setVisible(true);
        noButton.setVisible(true);
        dontKnowButton.setVisible(true);
        startGameButton.setVisible(false);
        restartGameButton.setVisible(false);
        gotoStatsticsButton.setVisible(false);
    }
    
    public void submitGuessState() {
        yesButton.setVisible(true);
        noButton.setVisible(true);
        dontKnowButton.setVisible(false);
        startGameButton.setVisible(false);
        restartGameButton.setVisible(false);
        gotoStatsticsButton.setVisible(false);
    }
    
    public void endGameState() {
        yesButton.setVisible(false);
        noButton.setVisible(false);
        dontKnowButton.setVisible(false);
        startGameButton.setVisible(false);
        restartGameButton.setVisible(true);
        gotoStatsticsButton.setVisible(true);
    }
    
	@Override
	public void actionPerformed(ActionEvent e) {
		FancyButton fancyButton = (FancyButton) e.getSource();
		if (fancyButton.equals(yesButton)) {
			model.setAnswer(AnswerEnum.YES);
		} else if (fancyButton.equals(noButton)) {
			model.setAnswer(AnswerEnum.NO);
		} else if (fancyButton.equals(dontKnowButton)) {
			model.setAnswer(AnswerEnum.DONT_KNOW);
		} else if (fancyButton.equals(startGameButton) || fancyButton.equals(restartGameButton)) {
			playState();
		}
	}
    
    private enum PlayActionType {
        Yes("Yes", "game-yes"),
        No("No", "game-no"),
        DontKnow("I Don't know", "game-dontknow"),
        StartGame("Start", "game-yes" /* TODO DAN change icon */),
        RestartGame("Restart", "game-replay"),
        GotoStatstics("Statistics", "game-statistics");
        
        private String title;
        private String icon;
        
        private PlayActionType(String title, String icon) {
            this.title = title;
            this.icon = icon;
        }
        
        public String getTitle() {
            return title;
        }
        
        public String getIcon() {
            return icon + ".png";
        }     
    }
    
}
